package menu.sys

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import org.apache.commons.io.FileUtils
import org.junit.Test
import java.io.File

/**
 * @author futao
 * Created on 2017/12/6 - 18:55.
 */
class Test3 {
    @Test
    fun testCase001() {

        val currentRoleType = 1
        val menuJsonObj = JsonParser().parse(FileUtils.readFileToString(File("C:\\Users\\11851\\Desktop\\menu.json"))).asJsonObject
        val oldRoleJsonObj = JsonParser().parse(FileUtils.readFileToString(File("C:\\Users\\11851\\Desktop\\role.json"))).asJsonObject

        val newRoleJsonObj = JsonObject()

        menuJsonObj.entrySet().forEach f1@ { (k, v) ->
            val menu = v.asJsonObject.getAsJsonObject("menu")


            //region menu
            val role_menu_all = JsonObject()
            val role_menu = JsonObject()
            role_menu_all.add("menu", role_menu)
            /*nav=true*/
            if (menu.get("nav").asBoolean) {
                /*有roleType*/
                if (menu.has("roleType")) {
                    /*roleType=currentRoleType*/
                    if (menu.get("roleType").asInt == currentRoleType) {
                        role_menu.apply {
                            add("name", menu.get("name"))
                            add("nav", menu.get("nav"))
                            add("title", menu.get("title"))
                            add("roleType", menu.get("roleType"))
                            menu.get("line")?.let { add("line", it) }
                            menu.get("style")?.let { add("style", it) }
                            menu.get("icon")?.let { add("icon", it) }
                        }
                    }
                    /*roleType!=currentRoleType*/
                    else {
                        return@f1
                    }
                }
                /*不包含roleType*/
                else {
                    role_menu.apply {
                        add("name", menu.get("name"))
                        add("nav", menu.get("nav"))
                        add("title", menu.get("title"))
                        menu.get("line")?.let { add("line", it) }
                        menu.get("style")?.let { add("style", it) }
                        menu.get("icon")?.let { add("icon", it) }
                    }
                }
            }
            /*nav=false*/
            else {
                return@f1
            }
            //endregion


            //region menuMap
            val menuMap = v.asJsonObject.getAsJsonObject("menuMap")

            val role_menuMap = JsonObject()
            menuMap.entrySet().forEach menuMap@ { (menuMap_k, menuMap_v) ->
                if (menuMap_v.asJsonObject.get("hide").asBoolean) {
                    return@menuMap
                } else {
                    if (menuMap.has("roleType")) {
                        if (menuMap_v.asJsonObject.get("roleType").asInt == currentRoleType) {
                            val new_role_menu_map = JsonObject()
                            new_role_menu_map.apply {
                                add("id", menuMap_v.asJsonObject.get("id"))
                                add("title", menuMap_v.asJsonObject.get("title"))
                                menuMap_v.asJsonObject.get("line")?.let { add("line", it) }
                                menuMap_v.asJsonObject.get("style")?.let { add("style", it) }
                                menuMap_v.asJsonObject.get("icon")?.let { add("icon", it) }
                                add("roleType", menuMap_v.asJsonObject.get("roleType"))
                            }
                            if (menuMap_v.asJsonObject.has("ctrl")) {
                                //val rightArray = JsonParser().parse(sysControllerListService.getRightCache(menuMap_v.asJsonObject.get("ctrl").asString).toString()).asJsonObject.get("items").asJsonArray
                            } else {

                            }
                            role_menuMap.add(menuMap_k, new_role_menu_map)
                        } else {
                            return@menuMap
                        }
                    } else {
                        val new_role_menu_map = JsonObject()
                        new_role_menu_map.apply {
                            add("id", menuMap_v.asJsonObject.get("id"))
                            add("title", menuMap_v.asJsonObject.get("title"))
                            menuMap_v.asJsonObject.get("line")?.let { add("line", it) }
                            menuMap_v.asJsonObject.get("style")?.let { add("style", it) }
                            menuMap_v.asJsonObject.get("icon")?.let { add("icon", it) }
                            add("roleType", menuMap_v.asJsonObject.get("roleType"))
                        }
                        role_menuMap.add(menuMap_k, new_role_menu_map)
                    }
                }
            }
            //endregion

            /*角色新权限菜单添加role_menu_all */
            role_menu_all.add("menuMap", role_menuMap)
            /*角色新权限菜单添加nav*/
            newRoleJsonObj.add(k, role_menu_all)
        }
        println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(newRoleJsonObj))
        FileUtils.writeStringToFile(File("C:\\Users\\11851\\Desktop\\newRole.json"), Gson().toJson(newRoleJsonObj), "utf-8")
    }

    val json = "{\n" +
            "      \"viewName\": \"\",\n" +
            "      \"name\": \"com.wise.wises.controller.client.ClientActivityController\",\n" +
            "      \"items\": [\n" +
            "        {\n" +
            "          \"viewName\": \"viewName001\",\n" +
            "          \"name\": \"com.wise.wises.controller.client.ClientActivityController:getData\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"viewName\": \"viewName001\",\n" +
            "          \"name\": \"com.wise.wises.controller.client.ClientActivityController:time\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"viewName\": \"viewName001\",\n" +
            "          \"name\": \"com.wise.wises.controller.client.ClientActivityController:consume\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"viewName\": \"viewName001\",\n" +
            "          \"name\": \"com.wise.wises.controller.client.ClientActivityController:postData\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"viewName\": \"viewName001\",\n" +
            "          \"name\": \"com.wise.wises.controller.client.ClientActivityController:getActivity\"\n" +
            "        }\n" +
            "      ]\n" +
            "    }"
}